package com.evolution.model;

import java.util.ArrayList;
import java.util.List;

import com.evolution.listeners.SpeciesListener;

public class SpeciesRegistry {

	private static SpeciesRegistry INSTANCE;
	
	private List<Species> registry;
	private List<SpeciesListener> listeners;
	
	public static SpeciesRegistry getInstance(){
		if(INSTANCE == null){
			INSTANCE = new SpeciesRegistry();
		}
		return INSTANCE;
	}
	
	private SpeciesRegistry(){
		registry = new ArrayList<Species>();
		listeners = new ArrayList<SpeciesListener>();
	}
	
	public Species getSpecies(int ID){
		if(ID < registry.size())
			return registry.get(ID);
		else
			return null;
	}
	
	public void registerSpecies(Species species){
		registry.add(species);
		species.setID(registry.size() - 1);
		
		for(SpeciesListener listener : listeners){
			listener.speciesRegistered(species);
		}
	}
	
	public void makeSpeciesExtinct(Species species){
		species.setExtinct(true);
		
		for(SpeciesListener listener : listeners){
			listener.speciesWentExtinct(species);
		}
	}
	
	public void addListener(SpeciesListener listener){
		listeners.add(listener);
	}
	
	public void removeListener(SpeciesListener listener){
		listeners.remove(listener);
	}
}
